Skip to content

Warn when Lexicon.remove names a reading no vocabulary file holds - #1536

Merged
derek73 merged 9 commits into
masterfrom
feat/issue-1535-remove-inert-warning
Nov 22, 2026
Merged

derek73 merged 9 commits into
masterfrom
feat/issue-1535-remove-inert-warning

Conversation

@derek73

@derek73 derek73 commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Closes #1535

Lexicon.remove warns where no vocabulary file holds the reading it names and no earlier add() of the lexicon gave it, so the edit can take effect under no choice of languages. It still returns the lexicon with the edit logged, as before.

>>> Lexicon.default().remove("alhaj", kind="religious")   # alhaj is a style
UserWarning: remove('alhaj', kind='religious') changes nothing: 'alhaj' has no 'religious' reading in any vocabulary file or earlier add(); it holds: style. Did you mean remove('alhaj', kind='style')?

The rule

What counts as held: for each word, every kind the reading index gives it (_reading_index.readings_of, every file's row whether on by default or not, the word looked up as remove stores it), plus every kind an earlier add in the same lexicon's log gave it. A category (kind="title", kind="name") counts where any of its kinds is held.

The message. Every message opens with the call and "changes nothing", so a caller can filter one by message. It then names one of five cases:

Case Example Message
(i) holds other kinds remove("alhaj", kind="religious") lists its kinds, and offers remove('alhaj', kind='style') only where one kind is plainly meant, never a pair kind (dean/particle offers nothing, don/title lists familiar and offers nothing)
(ii) only in the surnames or given_names list remove("김", kind="family") remove() does not edit the list; offers the dataclasses.replace that drops it
(iii) only the other name of other words' pairs remove("robert", kind="given"), Lexicon.empty().remove("abigail") offers remove('robert', kind='familiar'); pair names are found through the reading index as well as the compiled pairs
(iv) an acronym or dotted title written with its stops remove("Ph.D.", kind="credential"), stored ph.d offers remove('phd', kind='credential'); with no kind it names the kind the spelling is read by (M.D. gets md's credential reading, not its given reading); J.R. is no such spelling and offers nothing
(v) in no file and no earlier add remove("zzyzx", kind="title") "check its spelling, or, where a lexicon this one is joined with adds it, remove it from the joined lexicon"

A test runs each offered call and checks it takes what the inert one missed. A word named twice in one call warns once.

Case (iv) caught a real silent no-op: remove("Ph.D.", kind="credential"), the spelling a caller plausibly writes, took nothing, because remove stores ph.d while the vocabulary stores phd.

Silent, as the issue's Except lists:

  • a reading a file holds off by default (remove("piedade", kind="given"));
  • a category with one held kind (remove("dean", kind="title"));
  • the lexicon's own earlier add, removed once or twice;
  • a file's reading removed twice;
  • every=True;
  • the familiar and spelling pair removes.

Where it is raised: in remove() alone, through _warn_at_caller. A test checks the warning's filename and line through a plain call, a chained Lexicon.default().remove(...).remove(...) and Parser(lexicon=...). Replaying the log (dataclasses.replace, a pickle load, a parse) warns nothing again, and a test covers that too.

Accepted: a fragment meant to remove a word another lexicon adds warns, since remove cannot see the lexicon it will be joined with. Its caller filters that warning by its message, never by category, or does the remove on the joined lexicon; docs/customize.rst gives the recipe. A fragment removing a file's reading, which is how a pack is written, stays silent. Every shipped pack, plain and strict, builds warning-free, and a test holds that.

Where it fired in the repository

With pytest's warnings-as-errors, the full suite, sphinx-build -b doctest docs, python -m doctest README.rst and the packs were run. It fired in three tests and nowhere else (no docs page, README example, tool or pack). Each is a test that removes an inert reading on purpose. Each now takes pytest.warns with a match on the message and still asserts what it asserted before. These are the only expectation changes:

  • tests/v2/test_parser.py::test_a_packs_fragment_joins_under_the_callers_lexicon: pack bb's Lexicon.empty().remove("zzq", kind="title") takes out pack aa's added word when joined. This is case (ii), the accepted case above.
  • tests/v2/test_lexicon.py::test_a_later_edit_reads_after_an_earlier_one: Lexicon.empty().remove("x") before the add it precedes, case (ii).
  • tests/v2/test_match.py::test_a_remove_of_every_reading_of_a_word_keeps_the_pairs_other_words_hold: remove("zzrobert") with no kind, a name held only in another word's pair, case (iii).

The second silent no-op the issue grew from, docs/migrate.rst's alhaj recipe, was fixed by #1534 before this branch. On this tree it does not warn.

No parse moves

  • tools/differential/snapshot.jsonl is byte-identical.
  • tools/perf/call_count.py --against origin/master: 195.72 frames per parse on both, py3.11. remove is not on the parse path, and _pair_names, which scans the reading index, is cached and reached only on the warning path.
  • Full suite: 58,153 passed, 47 skipped, 3 xfailed on 3.11. On 3.15, in an environment of its own, the whole suite passed at b76658c (58,147), and the changed modules pass on the head.
  • mypy and ruff are clean; the doctest and HTML builds are silent; the README doctest passes.

Negative control

The new and changed tests were run against master's nameparser/_lexicon.py in a scratch copy. Every warning test fails there (11), and the silent cases pass either way.

Docs

  • Lexicon.remove's docstring: the rule, the three cases and the accepted fragment case.
  • docs/customize.rst, "Removing a word": the warning shown in a doctest, and the by-message filter recipe, with a title example whose output moves.
  • Release log: an Add bullet.
  • decisions/vocabulary-model: a 2026-11-22 Warn when Lexicon.remove names a reading no vocabulary file holds (remove("alhaj", kind="religious")) #1535 entry with the rule, where it is raised, why a warning, what is accepted, what was measured, and the issue's open question on add.
  • Root AGENTS.md's validation bullet: a recipe's doctest can pass with its edit doing nothing, so run the example without the edit.
  • migrate.rst: no note. A 3.1 caller's working code does not warn: the warning fires only on removes that already did nothing.
  • The issue's Open point (should an add of a reading the word already holds, with the same properties, warn too?) is settled as Declined, recorded in the decisions entry with the reason. An inert remove leaves a reading the caller wanted gone, so the parse is not what they asked for. A redundant add leaves the reading the caller wanted present, so the parse is what they asked for. Its usual cause is a recipe a later release made unnecessary, so a warning would fire on working code after every upgrade that ships a word a caller had added. Closes #1535 therefore closes it whole.
  • Sweep the 3.2 cycle's prose inside the code #1533's article helper (_a) is not used: no message here writes "a/an {kind}"; each quotes the kind ('religious').

Reviews

  1. First round, on b277dbf: code-reviewer, pr-test-analyzer (10 mutations, 8 killed; the two survivors gained rows: cased and dotted words, a multi-kind offer), comment-analyzer, silent-failure-hunter, design-docs-reviewer. Fixed in b76658c:
    • the offer could name an unrelated reading (dean/particle offered family), so it is now offered only where one kind is plainly meant, and never a pair kind;
    • a pair name with a kind, or on Lexicon.empty(), was told to check its spelling;
    • a word in the surnames list was told the same;
    • every message now shares one prefix, so the by-message filter covers each;
    • "or earlier add()" was added where the kinds came from an add;
    • the fragment remedy is now in the message;
    • the release-log bullet says 3.0.0 through 3.1.0;
    • the decisions entry gains its Standing line and Declined line, and lost the Open line;
    • the recipe-doctest lesson moved to docs/AGENTS.md's non-vacuous paragraph;
    • cased and dotted silent rows were added, and the dotted one found the Ph.D. no-op (case iv).
  2. Review of b76658c: the full-stop case offered another word (j.r. offered jr, though the parse never reads J.R. as jr) and ignored the kind asked; the list case ignored the kind. Fixed in 1836fb0.
  3. Review of 1836fb0: with no kind, M.D. offered remove('md'), which also takes Muhammad's given reading; the bare word's kinds missed the lexicon's own adds; a dead clause. Fixed in 612d7ec.
    • Left as a judgement call: a kind mismatch on a dotted or listed word (remove("m.d.", kind="title")) gets case (v)'s spelling advice. Nothing wrong is offered there.
  4. Review of 612d7ec: a dotted acronym or title fell to "in no vocabulary file" where no single kind was plain (us with two title kinds after an add; M.D. asked as given); the decisions entry's case (iv) condition was looser than the code; and no test ran the full-stop offers. Fixed in the head commit:
    • the message always names the bare word, offers the call where one kind is plain, and otherwise lists the kinds;
    • tests now run the M.D. offer, which takes the suffix and leaves Md. Ali Khan given, and an added acronym's offer.
    • Recorded as accepted: rules/S3's dotted shape still reads Ph.D. as a suffix after the offered remove takes the vocabulary's reading.
    • The head commit applies that review's findings with tests and was checked by the full suite; no fifth review round was run.

Not applicable: type-design-analyzer (no type added) and code-simplifier.

🤖 Generated with Claude Code

derek73 and others added 4 commits November 22, 2026 12:19
)

remove(word, kind=...) of a kind the word does not hold, or of a word in
no file, took nothing and said nothing: docs/migrate.rst's alhaj recipe
shipped that way, its doctest passing without the edit. remove() now
emits a UserWarning for each such word, naming the kinds it does hold
and offering the remove that would take one. A reading counts if any
file holds it, on by default or not (a choice may turn it on), or if an
earlier add() of the same lexicon gave it; a category counts where any
of its kinds is held. The warning is raised at the caller's own line
through _warn_at_caller, in remove() alone, so a lexicon replaying its
log (dataclasses.replace, a pickle load) never warns again. remove of
every word's readings and the familiar and spelling pair removes are
not judged.

Three tests removed a reading on purpose that the warning now names,
and assert it: a remove before the add it would undo, a no-kind remove
of a name held only as another word's pair, and a pack fragment
removing a reading another fragment adds, which remove() cannot see.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ecisions, AGENTS.md (#1535)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ame held only in pairs (#1535)

The message now tells three cases apart: a word in the vocabulary
without that kind (its kinds and the call that takes one), a word in
no file and no earlier add, and, with no kind, a word holding no
reading of its own, only the other name of another word's familiar or
spelling pair, for which it offers remove(word, kind="familiar") or
"spelling", the call that reaches pairs; a test runs that call and
checks the pair goes. The docstring, docs/customize.rst and the
decisions entry state the accepted case in the issue's terms: a
fragment meant to remove a word another lexicon adds warns, since
remove cannot see the lexicon it will be joined with, and its caller
filters that warning by its message, never by category, or removes on
the joined lexicon; customize.rst's recipe shows the filter, with a
title example whose output moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@derek73 derek73 added this to the 3.2 milestone Nov 22, 2026
@derek73 derek73 added enhancement vocabulary Fixed by a word-list entry: per-word vocabulary and language packs labels Nov 22, 2026
@derek73 derek73 self-assigned this Nov 22, 2026
derek73 and others added 5 commits November 22, 2026 12:39
…ove-inert-warning

# Conflicts:
#	tests/v2/test_lexicon.py
The messages:
- open with the call and "changes nothing" in every case, so the
  by-message filter covers them all;
- offer a call only where one kind is plainly meant, the single held
  kind of the asked category or else the single held kind, and never a
  familiar or spelling kind (dean/particle offered the family reading,
  don/title a pair remove);
- say "in any vocabulary file or earlier add()", the add counting;
- find a pair's other name through the reading index as well as the
  compiled pairs, so robert with a kind and abigail on Lexicon.empty()
  read as pair names, not as words to check the spelling of;
- tell a word held only in the surnames or given_names list how to
  drop it, with code a test runs;
- tell Ph.D., which remove stores as ph.d and the vocabulary as phd,
  to remove phd: a real no-op the cased and dotted rows turned up;
- name the joined-lexicon remedy beside the spelling one;
- warn once for a word named twice.

Docs: remove's docstring states the cases and that a joined lexicon's
adds count; customize.rst's lead names the two common cases, its
quoted message updated; the release-log bullet says 3.0.0 through
3.1.0 and keeps one limit sentence; the decisions entry states the
five cases and the offer rule, declines an add-repeat warning, which
leaves the reading the caller wanted, replaces the Open line, and
gains its Standing line; the recipe-doctest lesson moves from root
AGENTS.md's validation bullet into docs/AGENTS.md's non-vacuous
paragraph, where a docs writer meets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of the fix commit found the full-stop case offering another
word: remove("j.r.") was offered remove("jr"), though the parse never
reads J.R. as jr, and that remove changes how Jr. reads; and it ignored
the kind asked, so remove("m.d.", kind="title") offered a remove that
takes nothing either. It now fires only where the bare word is a suffix
acronym or a dotted title, the spellings the parse reads with their
stops dropped, and holds the kind asked; else the word is reported as
in no file. The list case offers dropping the entry only where the kind
asked is the list's (family or name for the surnames, given or name for
the given names). Test rows hold j.r. and m.d. as a title in case (v).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…'s adds there (#1535)

Review of the narrowing found remove("M.D.") offering remove("md"),
which also takes md's given reading (Muhammad written short) and moves
"Md. Ali Khan"; with no kind asked the full-stop case now names the
kind the dotted spelling is read by, the suffix acronym's suffix
reading or the dotted title's title one, offering
remove("md", kind="credential"). The bare word's kinds now count the
lexicon's own earlier adds, as the word's do, so an acronym a caller
added (xq) is offered and not reported as in no file. The list case's
third test, which could never hold, is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… title (#1535)

Review of 612d7ec found a dotted acronym or title falling to "in no
vocabulary file" where no call was plainly meant (us read by two title
kinds after a caller's add; M.D. asked as given, Ph.D. as title); the
message now always names the bare word, offers the call where one kind
is plainly meant, and otherwise lists the kinds the dotted spelling is
read by. Tests now run the full-stop offers: remove('md',
kind='credential') takes M.D.'s suffix and leaves "Md. Ali Khan"
given, and an added acronym's offer takes it. The decisions entry
states case (iv)'s condition as the code reads it, and records as
accepted that rules/S3's dotted shape still reads Ph.D. as a suffix
after the offered remove takes the vocabulary's reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@derek73
derek73 merged commit bd9c62f into master Nov 22, 2026
14 checks passed
@derek73
derek73 deleted the feat/issue-1535-remove-inert-warning branch November 22, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement vocabulary Fixed by a word-list entry: per-word vocabulary and language packs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when Lexicon.remove names a reading no vocabulary file holds (remove("alhaj", kind="religious"))

1 participant